﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class AdminPortal_Controls_FileUpload : System.Web.UI.UserControl
{
    string _strFolder = "~/upload/images/";

    public bool setTextPath
    {
        set { txtFileName.ReadOnly = value; }
    }

    public string UploadFolder
    {
        get { return _strFolder; }
        set { _strFolder = value; }
    }

    public string FilePath
    {
        get { return txtFileName.Text; }
        set { txtFileName.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(_strFolder))
        {
            _strFolder = _strFolder.Replace('\\', '/');
            if (!_strFolder.EndsWith("/")) _strFolder += "/";
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            lblError.Visible = false;
            if (fulUpload.HasFile)
            {
                string sPath;
                string strDefaultFolder = Server.MapPath(_strFolder);
                string strFileName;

                strFileName = System.IO.Path.GetFileName(fulUpload.PostedFile.FileName);

                if (!Directory.Exists(strDefaultFolder)) Directory.CreateDirectory(strDefaultFolder);

                sPath = _strFolder + strFileName;
                fulUpload.SaveAs(Server.MapPath(sPath));
                txtFileName.Text = sPath;
            }
            fulUpload.Dispose();
        }
        catch (Exception exc)
        {
            lblError.Visible = true;
            lblError.Text = exc.Message;
        }
    }
}